<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;

class News extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\News';

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '标题', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'images', 'title' => '缩略图', 'width' => 90,
                'templet' => '<div><img class="thumbnail" src="{{d.images}}"/></div>',
                'format' => 'img', 'modify' => 'add|edit'])
            ->addRow(['field' => 'type', 'title' => '类别', 'width' => 120, 'format' => 'select',
                'value' => [['id' => 1, 'title' => '新闻'], ['id' => 2, 'title' => '公告']],
                'modify' => 'add|edit'])
            ->addRow(['field' => 'content', 'title' => '详情', 'format' => 'rich_text', 'modify' => 'add|edit'])
            ->addRow(['field' => 'hits', 'title' => '浏览量', 'width' => 120])
            ->addRow(['field' => 'sort', 'title' => '排序', 'width' => 120, 'modify' => 'add|edit', 'format' => 'input',
                'templet' => '<div>
                <input type="text" data-key="sort" data-id="{{d.id}}" data-url="' . url('update') . '" value="{{d.sort}}" class="layui-input x-sort tableInput"/>
                </div>'
            ])
            ->addRow(['field' => 'create_time', 'title' => '创建时间'])
            ->addRow(['field' => 'update_time', 'title' => '更新时间'])
            ->addRow(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 1000, 'height' => 1000], 'delete'])
            ->addTopButtons(['add', 'deleteAll'])
            ->setSearch($this->setSearch())
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['title', '文章标题搜索', 'input', '', '', '', 'like']);
    }



}
